package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.security.filter.JwtAuthenticationTokenFilter;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

import static com.zzyl.common.utils.PageUtils.startPage;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
         return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    public int handleAlertData(AlertDataDto alertDataDto) {
        // 根据id查询报警数据
        AlertData alertData = alertDataMapper.selectAlertDataById(alertDataDto.getId());
        if (alertData == null){
            return  0;
        }
        // 设置处理状态为已处理
        alertData.setStatus(1);
        // 设置处理结果
        alertData.setProcessingResult(alertDataDto.getProcessingResult());

        // 设置处理时间
        if (alertDataDto.getProcessingTime() != null) {
            alertData.setProcessingTime(java.time.Instant.parse(alertDataDto.getProcessingTime())
                    .atZone(java.time.ZoneOffset.UTC)
                    .toLocalDateTime());
        } else {
            alertData.setProcessingTime(LocalDateTime.now(ZoneId.of("UTC")));
        }

       alertData.setProcessorId(SecurityUtils.getUserId());

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", alertData.getProcessorId());
        SysUser sysUser = sysUserMapper.selectUserById(alertData.getProcessorId());
        alertData.setProcessorName(sysUser.getNickName());

        //更新
        return alertDataMapper.updateAlertData(alertData);
    }
}
